﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Percentile.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace BOL.Linq.Descriptive
{
    public static partial class Descriptive
    {
        public static decimal Percentile(this IEnumerable<decimal> source, double p)
        {
            return (decimal)source.Select(x => (double)x).Percentile(p);
        }

        public static double Percentile(this IEnumerable<double> source, double p)
        {
            if (p < 0 || p > 1)
                throw new ArgumentOutOfRangeException();

            var list = source.ToList();
            var count = list.Count();
            var sorted = list.OrderBy(x => x);

            var n = p * (count - 1) + 1.0;
            var k = (int)Math.Floor(n); // integer component
            var d = n - k; // decimal component

            if (k == 0)
                return sorted.ElementAt(0);
            
            if (k == count)
                return sorted.ElementAt(count - 1);
            
            return sorted.ElementAt(k - 1) + d * (sorted.ElementAt(k) - sorted.ElementAt(k - 1));
        }

        public static float Percentile(this IEnumerable<float> source, double p)
        {
            return (float)source.Select(x => (double)x).Percentile(p);
        }

        public static double Percentile(this IEnumerable<int> source, double p)
        {
            return source.Select(x => (double)x).Percentile(p);
        }

        public static double Percentile(this IEnumerable<long> source, double p)
        {
            return source.Select(x => (double)x).Percentile(p);
        }

        public static decimal Percentile<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector, double p)
        {
            return source.Select(selector).Percentile(p);
        }

        public static double Percentile<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector, double p)
        {
            return source.Select(selector).Percentile(p);
        }

        public static float Percentile<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector, double p)
        {
            return source.Select(selector).Percentile(p);
        }

        public static double Percentile<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector, double p)
        {
            return source.Select(selector).Percentile(p);
        }

        public static double Percentile<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector, double p)
        {
            return source.Select(selector).Percentile(p);
        }
    }
}
